package util.toolkit;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.enterprise.inject.Model;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 用于生成时间段（2015-09-01 -- 2016-09-01）
 * @author 徐震霖
 * @since 2018-11-07 19:41
 *
 */
public class Period {
	
	/**
	 * 获取时间阶段JSON的类
	 * @param starTime 开始时间
	 * @return 返回一个时间阶段JSON
	 */
	public static List<model.Period> getPeriod(String starTime){
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
		String currentTime = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
		List<model.Period> periodList = new ArrayList<model.Period>();
		JSONObject result = new JSONObject();
		
		try {
			int moth = getMonth(df.parse(starTime), df.parse(currentTime));
			System.out.println(moth);
			if(moth % 12 == 0){
				for (int i = 0; i < moth % 12; i++) {
					model.Period period = new model.Period();
					if(i == 0){
						period.setId(i);
						period.setStarTime(starTime);
						period.setEndTime((Integer.parseInt(starTime.substring(0, 4)) + 1) + "-09-01");
					}
					else {
						period.setId(i);
						period.setStarTime((Integer.parseInt(starTime.substring(0, 4)) + i) + "-09-01");
						period.setEndTime((Integer.parseInt(starTime.substring(0, 4)) + i + 1) + "-09-01");
					}
					periodList.add(period);
				}				
			}
			else {
				for (int i = 0; i < moth % 12 + 1; i++) {
					model.Period period = new model.Period();
					if(i == 0){
						period.setId(i);
						period.setStarTime(starTime);
						period.setEndTime((Integer.parseInt(starTime.substring(0, 4)) + 1) + "-09-01");
					}
					else {
						period.setId(i);
						period.setStarTime((Integer.parseInt(starTime.substring(0, 4)) + i) + "-09-01");
						period.setEndTime((Integer.parseInt(starTime.substring(0, 4)) + i + 1) + "-09-01");
					}
					periodList.add(period);
				}	
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		result.put("code", ResponseJSON.FLAG_SUCC);
		result.put("msg", "查询成功");
		result.put("count", periodList.size());
		JSONArray jsonArray = JSONArray.fromObject(periodList);
		result.element("data", jsonArray);
		
		return periodList;
	}


	/**
     * 获取两个日期相差几个月
     * @author 徐震霖
     * @date 2018-11-07 20:04
     * @param start 开始时间
     * @param end 结束时间
     * @return 返回月数(如2015-09-01 —— 2018-09-02  则相差37个月)
     */
    public static int getMonth(Date start, Date end) {
		if (start.after(end)) {
			Date t = start;
			start = end;
			end = t;
		}
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(start);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(end);
		Calendar temp = Calendar.getInstance();
		temp.setTime(end);
		temp.add(Calendar.DATE, 1);
 
		int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
		int month = 0;
		if(endCalendar.get(Calendar.MONTH) != startCalendar.get(Calendar.MONTH)){
			if(endCalendar.get(Calendar.DAY_OF_MONTH) > startCalendar.get(Calendar.DAY_OF_MONTH)){
				month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH) + 1;
			}
			else {
				month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
			}
		}
		else {
			month = 1;
		}
 
		if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) == 1)) {
			return year * 12 + month + 1;
		} else if ((startCalendar.get(Calendar.DATE) != 1) && (temp.get(Calendar.DATE) == 1)) {
			return year * 12 + month;
		} else if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) != 1)) {
			return year * 12 + month;
		} else {
			return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
		}
    }
	
	public static void main(String[] args) {
		
		//String date=new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
		System.out.println(getPeriod("2015-09-01"));

	}

}
